#include "croot.h"
#include <X11/Xatom.h>

#include <buola/gui/x11/cx11window.h>
#include <buola/gui/x11/cx11toolkit.h>
#include <buola/gui/x11/cx11display.h>

#include "cclient.h"

namespace buola { namespace bwm {

void CRoot::BuolaSetWMExtensions()
{
    Atom lAtom[100];

    int i=0;

    lAtom[i++]=gui::x11::get_default_x11_display()->Atoms().mBuolaCustomDeco;

    mX11Window->SetWMProperty(gui::x11::get_default_x11_display()->Atoms().mBuolaWMExtensions,lAtom,i);
}

void CRoot::NETSetDesktopInfo()
{
    int lCount=mDesktops.mCount;

    mX11Window->SetWMProperty(gui::x11::get_default_x11_display()->Atoms().mNetNumberOfDesktops,lCount);

    int lGeometry[2]={(int)GetSize().x,(int)GetSize().y};
    mX11Window->SetWMProperty(gui::x11::get_default_x11_display()->Atoms().mNetDesktopGeometry,lGeometry,2);

    int lViewport[lCount*2];
    int lWorkArea[lCount*4];

    for(int i=0;i<lCount;i++)
    {
        lViewport[i*2]=(int)mDesktops.mViewport[i].x;
        lViewport[i*2+1]=(int)mDesktops.mViewport[i].x;
        lWorkArea[i*4]=(int)mDesktops.mWorkArea[i].l;
        lWorkArea[i*4+1]=(int)mDesktops.mWorkArea[i].t;
        lWorkArea[i*4+2]=(int)mDesktops.mWorkArea[i].w();
        lWorkArea[i*4+3]=(int)mDesktops.mWorkArea[i].h();
    }

    mX11Window->SetWMProperty(gui::x11::get_default_x11_display()->Atoms().mNetDesktopViewport,lViewport,2*lCount);
    mX11Window->SetWMProperty(gui::x11::get_default_x11_display()->Atoms().mNetWorkArea,lWorkArea,4*lCount);
}

void CRoot::NETSetDesktopNames()
{
    int lNeededLength=0;

    for(int i=0;i<mDesktops.mCount;i++)
    {
        lNeededLength+=mDesktops.mName[i].length()+1;
    }

    char lString[lNeededLength];

    int lPos=0;
    for(int i=0;i<mDesktops.mCount;i++)
    {
        for(int j=0;j<mDesktops.mName[i].length()+1;j++,lPos++)
        {
            lString[lPos]=mDesktops.mName[i][j];
        }
    }

    gui::x11::get_default_x11_display()->SetWindowProperty(GetID(),gui::x11::get_default_x11_display()->Atoms().mNetDesktopNames,gui::x11::get_default_x11_display()->Atoms().mUTF8,
                                    8,lNeededLength,(void*)lString);
}

void CRoot::NETSetCurrentDesktop()
{
    mX11Window->SetWMProperty(gui::x11::get_default_x11_display()->Atoms().mNetCurrentDesktop,mDesktops.mActive);
}

void CRoot::NETSetClientList()
{
    int lCount=mClients.size();
    Window lList[lCount];

    int lNum=0;
    for(int i=0;i<lCount;i++)
    {
        if(!mClients[i]->mID) continue;
        if(!mClients[i]->GetFlag(CClient::FLAG_MANAGED)) continue;
        lList[lNum++]=mClients[i]->mID;
    }

    msg_debug<12>() << "Adding " << lCount << " windows\n";

    gui::x11::get_default_x11_display()->SetWindowProperty(GetID(),gui::x11::get_default_x11_display()->Atoms().mNetClientList,XA_WINDOW,
                        32,lNum,(unsigned char*)lList);
}

void CRoot::NETSetStackingList()
{
    int lCount=mClientsStack.size();
    Window lList[lCount];

    int lNum=0;
    for(int i=0;i<lCount;i++)
    {
        if(!mClientsStack[i]->mID) continue;
        if(!mClientsStack[i]->GetFlag(CClient::FLAG_MANAGED)) continue;
        lList[lNum++]=mClientsStack[i]->mID;
    }

    msg_debug<15>() << "Adding stack " << lNum << " windows\n";

    gui::x11::get_default_x11_display()->SetWindowProperty(GetID(),gui::x11::get_default_x11_display()->Atoms().mNetClientListStacking,XA_WINDOW,
                    32,lNum,(unsigned char*)lList);
}

/*namespace bwm*/ } /*namespace buola*/ }
